package com.gzsxy.esjy.service.netty.fileServer.configure.properties;


import com.gzsxy.esjy.service.netty.client.ProxyConfig;
import com.gzsxy.esjy.service.netty.client.ProxyTarget;
import com.gzsxy.esjy.service.netty.fileServer.configure.ServerStaticProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.List;

/**
 * @author zhangxiaolong
 * @date 2022/7/11 9:36
 */
@Configuration
public class FileProperties {

    private static final String HTTP_SEPARATOR = "/";

    /**
     * 资源uri前缀
     */
    @Value("${server.resources.uri.prefix}")
    private String resourcesPrefix;

    /**
     * 绝对路径资源uri前缀
     */
    @Value("${server.absolute.resources.uri.prefix:/resources/absolute}")
    private String absoluteResourcesPrefix;

    /**
     * 资源保存根路径
     */
    @Value("${server.resources.root}")
    private String resourcesRoot;

    /**
     * 资源请求额外线程启动
     */
    @Value("${server.resources.extralThread:false}")
    private boolean threadEnabled;

    /**
     * 是否启用代理
     */
    @Value("${server.resources.proxy.enabled:false}")
    private boolean proxyEnabled;

    /**
     * 最大转发响应报文
     */
    @Value("#{${server.resources.proxy.responseMax:102400}*1024}")
    private long responseMax;


    @Value("${server.port}")
    private int port;

    @Value("${server.id}")
    private String id;

    @Autowired(required = false)
    private ProxyConfig proxyConfig;


    @PostConstruct
    private void init() {
        if (!resourcesPrefix.endsWith(HTTP_SEPARATOR)) {
            resourcesPrefix += HTTP_SEPARATOR;
        }
        if (!resourcesPrefix.startsWith(HTTP_SEPARATOR)) {
            resourcesPrefix = HTTP_SEPARATOR + resourcesPrefix;
        }

        File file = new File(resourcesRoot);
        if (file.exists()) {
            file.mkdirs();
        }
        String absolutePath = file.getAbsolutePath();
        if (!File.separator.equals(HTTP_SEPARATOR)) {
            absolutePath = absolutePath.replaceAll("\\\\", HTTP_SEPARATOR);
        }
        if (!absolutePath.endsWith(HTTP_SEPARATOR)) {
            absolutePath += HTTP_SEPARATOR;
        }
        resourcesRoot = absolutePath;

        //常量化配置文件
        ServerStaticProperties.MAX_TRANFER_BODY = responseMax;
        ServerStaticProperties.RESOURCES_PREFIX = resourcesPrefix;
        ServerStaticProperties.ABSOLUTE_RESOURCES_PREFIX = absoluteResourcesPrefix;
        ServerStaticProperties.RESOURCES_ROOT = resourcesRoot;
        ServerStaticProperties.THREAD_ENABLED = threadEnabled;
        ServerStaticProperties.PROXY_ENABLED = proxyEnabled;
        ServerStaticProperties.THIS_SYSTEM = id;

        if (proxyConfig != null) {
            List<ProxyTarget> targets = proxyConfig.getTargets();
            for (ProxyTarget proxyTarget : targets) {
                if (!proxyTarget.getId().equals(id)) {
                    ServerStaticProperties.PROXY_TARGET.put(proxyTarget.getId(), proxyTarget);
                }
            }
        }

    }


}
